package models

import "time"

type Warning struct {
	ID int64 `gorm:"primary_key" json:"id"`
	PointId int `json:"pointId" gorm:"point_id"`
	BSUId int `json:"bsuId" gorm:"column:bsu_id;default:null"`
	BCUId int `json:"bcuId" gorm:"column:bcu_id;default:null"`
	BMUId int `json:"bmuId" gorm:"column:bmu_id"`
	ValueType int `json:"valueType" gorm:"value_type"`
	Value string `json:"value"`
	CollTime time.Time `json:"collTime" gorm:"column:coll_time;default:null"`
	Status int `json:"status" gorm:"default:null"`
	Operator int `json:"operator" gorm:"default:null"`
	Operation int `json:"operation" gorm:"default:null"`
	OperTime time.Time `json:"operTime" gorm:"column:oper_time;default:null"`
}

func ExistWarningById(id int) bool {
	var warning Warning
	db.Select("id").Where("id = ?", id).First(&warning)
	return warning.ID > 0
}

func GetWarningsByBSUId(bsuId int) (warnings []Warning) {
	db.Where("bsu_id = ?", bsuId).Find(&warnings)
	return
}

func GetWarningsByBCUId(bcuId int) (warnings []Warning) {
	db.Where("bcu_id = ?", bcuId).Find(&warnings)
	return
}

func GetWarningsByBMUId(bmuId int) (warnings []Warning) {
	db.Where("bmu_id = ?", bmuId).Find(&warnings)
	return
}

func AddWarningInBMU(pointId, bmuId, valueType int, value string) bool {
	db.Create(&Warning{
		PointId: pointId,
		BMUId: bmuId,
		ValueType: valueType,
		Value: value,
	})
	return true
}

func AddWarningInBCU(pointId, bmuId, bcuId, valueType int, value string) bool {
	db.Create(&Warning{
		PointId: pointId,
		BMUId: bmuId,
		BCUId: bcuId,
		ValueType: valueType,
		Value: value,
	})
	return true
}

func AddWarningInBSU(pointId, bmuId, bcuId, bsuId, valueType int, value string) bool {
	db.Create(&Warning{
		PointId: pointId,
		BMUId: bmuId,
		BCUId: bcuId,
		BSUId: bsuId,
		ValueType: valueType,
		Value: value,
	})
	return true
}

func UpdateWarning(id int, data interface{}) bool {
	db.Model(&Warning{}).Where("id = ?", id).Updates(data)
	return true
}

func DeleteWarning(id int) bool {
	db.Where("id = ?", id).Delete(&Warning{})
	return true
}